package com.ezia.coreservices.charging.controller;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.ezia.coreservices.charging.config.MobilityAsiaConfig;
import com.ezia.coreservices.charging.config.StarCharingConfig;
import com.ezia.coreservices.charging.utils.AesEncryptionUtil;

import net.sf.json.JSONObject;

@RestController
public class StarChargingPushController {

	
	private static final Logger logger = LoggerFactory.getLogger(StarChargingPushController.class);
	
	@Autowired
	private StarCharingConfig starcharingconfig;
	
	@Autowired
	private AesEncryptionUtil aesencryptionutil;
	
	@Autowired
	private MobilityAsiaConfig mobilityasiaconfig;

	/**
	 * 互联互通控制推送接口：推送启动充电结果
	 * 
	 * @param token
	 * @return 
	 * add by yc
	 * @throws Exception 
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "notification_start_charge_result",method = RequestMethod.POST)
	public Object notification_start_charge_result(@RequestBody String RequestData) throws Exception {
		logger.info("请求推送接口");
		logger.info(RequestData);
		JSONObject resultMsg = new JSONObject();
		JSONObject requestData = JSONObject.fromObject(RequestData);
		Map<String, Object> mResponse = (Map<String, Object>) JSONObject.fromObject(requestData.toString());
		mResponse.put("Data", aesencryptionutil.decryptData(mResponse.get("Data").toString(), mobilityasiaconfig.getDataSecret(),mobilityasiaconfig.getDataSecretIV()));
		Map<String, Object> mData = (Map<String, Object>) JSONObject.fromObject(mResponse.get("Data").toString());
		if(mData.get("StartChargeSeqStat").toString()=="2") {
			resultMsg.put("StartChargeSeq", mData.get("StartChargeSeq"));
			resultMsg.put("SuccStat", 0);
			resultMsg.put("FailReason", 0);
			Map<String, Object> mEncryptData = new HashMap<String, Object>();
			mEncryptData.putAll(resultMsg);
			String newdata = aesencryptionutil.encryptData(mEncryptData,mobilityasiaconfig.getDataSecret(),mobilityasiaconfig.getDataSecretIV());
			Map<String, Object> mRequest = new HashMap<String, Object>();
			mRequest.put("Ret", 0);
			mRequest.put("Data", newdata);
			mRequest.put("Msg", "");
			mRequest.put("Sig", aesencryptionutil.getSign(newdata, aesencryptionutil.getTimestamp(), mobilityasiaconfig.getSigSecret(),"313744932", mobilityasiaconfig.getSEQ()));
			JSONObject result = JSONObject.fromObject(mRequest);
			return result;
		}else {
			resultMsg.put("StartChargeSeq", mData.get("StartChargeSeq"));
			resultMsg.put("SuccStat", 1);
			resultMsg.put("FailReason", 1);
			Map<String, Object> mEncryptData = new HashMap<String, Object>();
			mEncryptData.putAll(resultMsg);
			String newdata = aesencryptionutil.encryptData(mEncryptData,mobilityasiaconfig.getDataSecret(),mobilityasiaconfig.getDataSecretIV());
			Map<String, Object> mRequest = new HashMap<String, Object>();
			mRequest.put("Ret", 0);
			mRequest.put("Data", newdata);
			mRequest.put("Msg", "");
			mRequest.put("Sig", aesencryptionutil.getSign(newdata, aesencryptionutil.getTimestamp(), mobilityasiaconfig.getSigSecret(),"313744932", mobilityasiaconfig.getSEQ()));
			JSONObject result = JSONObject.fromObject(mRequest);
			return result;
		}
	}
	
	/**
	 * 互联互通控制推送接口：推送充电订单结果
	 * 
	 * @param token
	 * @return 
	 * add by yc
	 * @throws Exception 
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "notification_charge_order_info",method = RequestMethod.POST)
	public Object notification_charge_order_info(@RequestBody String RequestData) throws Exception {
		logger.info("请求订单推送接口");
		logger.info(RequestData);
		JSONObject resultMsg = new JSONObject();
		JSONObject requestData = JSONObject.fromObject(RequestData);
		Map<String, Object> mResponse = (Map<String, Object>) JSONObject.fromObject(requestData.toString());
		mResponse.put("Data", aesencryptionutil.decryptData(mResponse.get("Data").toString(), mobilityasiaconfig.getDataSecret(),mobilityasiaconfig.getDataSecretIV()));
		Map<String, Object> mData = (Map<String, Object>) JSONObject.fromObject(mResponse.get("Data").toString());
		if(mData.get("StartChargeSeqStat").toString()=="2") {
			resultMsg.put("StartChargeSeq", mData.get("StartChargeSeq"));
			resultMsg.put("SuccStat", 0);
			resultMsg.put("FailReason", 0);
			Map<String, Object> mEncryptData = new HashMap<String, Object>();
			mEncryptData.putAll(resultMsg);
			String newdata = aesencryptionutil.encryptData(mEncryptData,mobilityasiaconfig.getDataSecret(),mobilityasiaconfig.getDataSecretIV());
			Map<String, Object> mRequest = new HashMap<String, Object>();
			mRequest.put("Ret", 0);
			mRequest.put("Data", newdata);
			mRequest.put("Msg", "");
			mRequest.put("Sig", aesencryptionutil.getSign(newdata, aesencryptionutil.getTimestamp(), mobilityasiaconfig.getSigSecret(),"313744932", mobilityasiaconfig.getSEQ()));
			JSONObject result = JSONObject.fromObject(mRequest);
			return result;
		}else {
			resultMsg.put("StartChargeSeq", mData.get("StartChargeSeq"));
			resultMsg.put("SuccStat", 1);
			resultMsg.put("FailReason", 1);
			Map<String, Object> mEncryptData = new HashMap<String, Object>();
			mEncryptData.putAll(resultMsg);
			String newdata = aesencryptionutil.encryptData(mEncryptData,mobilityasiaconfig.getDataSecret(),mobilityasiaconfig.getDataSecretIV());
			Map<String, Object> mRequest = new HashMap<String, Object>();
			mRequest.put("Ret", 0);
			mRequest.put("Data", newdata);
			mRequest.put("Msg", "");
			mRequest.put("Sig", aesencryptionutil.getSign(newdata, aesencryptionutil.getTimestamp(), mobilityasiaconfig.getSigSecret(),"313744932", mobilityasiaconfig.getSEQ()));
			JSONObject result = JSONObject.fromObject(mRequest);
			return result;
		}
		
		
	}
}
